﻿using sentry.Tool;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sentry.Model
{
    /// <summary>
    /// 任务信息
    /// </summary>
    public class TaskInfo
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 运行目录
        /// </summary>
        public string Directory { get; set; }
        /// <summary>
        /// 执行命令
        /// </summary>
        public string Command { get; set; }
        /// <summary>
        /// 是否自启动
        /// </summary>
        public bool AutoStart { get; set; }
        /// <summary>
        /// 是否自动重启
        /// </summary>
        public bool AutoRestart { get; set; }
        /// <summary>
        /// 自动重启时间
        /// </summary>
        public string RestartTime { get; set; }
        /// <summary>
        /// 错误日志文件
        /// </summary>
        public string StderrLogfile { get; set; }
        /// <summary>
        /// 输出日志文件
        /// </summary>
        public string StdoutLogfile { get; set; }
        /// <summary>
        /// 进程id
        /// </summary>
        public int Pid { get; set; }
        /// <summary>
        /// 任务状态 0:stop 1:running
        /// </summary>
        public int Status { get; set; }
        /// <summary>
        /// 任务启动的时间
        /// </summary>
        public DateTime RunTime { get; set; }

        /// <summary>
        /// 任务
        /// </summary>
        public Process Task { get; set; }
        /// <summary>
        /// 任务管理者
        /// </summary>
        private ProcessManager _tasker = null;
        /// <summary>
        /// 发生错误并重启的次数
        /// </summary>
        public int ErrorRunCount { get; set; }
        /// <summary>
        /// 发生错误并重启的时间
        /// </summary>
        public DateTime ErrorRunTime { get; set; } = DateTime.MinValue;
        /// <summary>
        /// 自动重启监测通过的分钟数
        /// </summary>
        private int autoRestartCheckPassMinute = -1;

        #region 当前是否重启时间
        /// <summary>
        /// 当前是否重启时间
        /// </summary>
        /// <returns></returns>
        public bool GetIsAutoRestartTime()
        {
            DateTime dtNow = DateTime.Now;
            if (autoRestartCheckPassMinute == dtNow.Minute)
            {
                return false;
            }
            else
            {
                autoRestartCheckPassMinute = -1;
            }

            string time = RestartTime;
            if (string.IsNullOrEmpty(time))
            {
                time = "* * * * *";
            }
            time = time.Trim();

            string minute = "*";
            string hour = "*";
            string day = "*";
            string month = "*";
            string week = "*";

            int index1 = time.IndexOf(' ');
            if (index1 > 0)
            {
                minute = time.Substring(0, index1);
                time = time.Substring(index1 + 1, time.Length - index1 - 1).Trim();
                int index2 = time.IndexOf(' ');
                if (index2 > 0)
                {
                    hour = time.Substring(0, index2);
                    time = time.Substring(index2 + 1, time.Length - index2 - 1).Trim();

                    int index3 = time.IndexOf(' ');
                    if (index3 > 0)
                    {
                        day = time.Substring(0, index3);
                        time = time.Substring(index3 + 1, time.Length - index3 - 1).Trim();

                        int index4 = time.IndexOf(' ');
                        if (index4 > 0)
                        {
                            month = time.Substring(0, index4);
                            week = time.Substring(index4 + 1, time.Length - index4 - 1).Trim();
                        }
                    }
                }
            }
            bool isok = true;
            if (minute != "*")
            {
                int[] arrayMinute = minute.Split(',').Select(t => int.Parse(t)).ToArray();
                if (!arrayMinute.Contains(dtNow.Minute))
                {
                    isok = false;
                }
            }
            else
            {
                isok = false;
            }

            if (hour != "*")
            {
                int[] arrayHour = hour.Split(',').Select(t => int.Parse(t)).ToArray();
                if (!arrayHour.Contains(dtNow.Hour))
                {
                    isok = false;
                }
            }

            if (day != "*")
            {
                int[] arrayDay = day.Split(',').Select(t => int.Parse(t)).ToArray();
                if (!arrayDay.Contains(dtNow.Day))
                {
                    isok = false;
                }
            }

            if (month != "*")
            {
                int[] arrayMonth = month.Split(',').Select(t => int.Parse(t)).ToArray();
                if (!arrayMonth.Contains(dtNow.Month))
                {
                    isok = false;
                }
            }

            if (week != "*")
            {
                int[] arrayWeek = week.Split(',').Select(t => int.Parse(t)).ToArray();
                if (!arrayWeek.Contains((int)dtNow.DayOfWeek))
                {
                    isok = false;
                }
            }

            if (minute == "*" &&
                hour == "*" &&
                day == "*" &&
                month == "*" &&
                week == "*")
            {
                isok = false;
            }

            if (isok)
            {
                autoRestartCheckPassMinute = dtNow.Minute;
            }

            return isok;
        } 
        #endregion

        public Process RunTask()
        {
            return _tasker.RunTask();
        }

        public bool Kill()
        {
            return _tasker.Kill(Task);
        }

        public TaskInfo()
        {
            _tasker = new ProcessManager(this);
        }
    }
}
